package dk.isten.andro.main;

import dk.isten.andro.R;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Toast;

public class yellow extends SuperActivity implements SensorEventListener {
    /** Called when the activity is first created. */
    
	MediaPlayer enLyd;
	String[] senesteMålinger = new String[12];
    SensorManager sensorManager;
    private long lastUpdate;
    int nuLydStyrke;
    AudioManager audiomanager;
    
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yellow);
                
        audiomanager = (AudioManager) this.getSystemService(AUDIO_SERVICE);
        //skru op for lyden
        nuLydStyrke = audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC); 
        int  maxLydStyrke = audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 

        audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC, maxLydStyrke, AudioManager.FLAG_SHOW_UI);
        
       
        enLyd = MediaPlayer.create(this, R.raw.ost);
        enLyd.setVolume(1,1);
        enLyd.start();
        
        
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
	}
        protected void onResume() {
    		super.onResume();

    		

    		sensorManager.registerListener(this,
    				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    				SensorManager.SENSOR_DELAY_NORMAL);
    		lastUpdate = System.currentTimeMillis();
     	}

    	@Override
    	protected void onPause() {
    		super.onPause();
    		sensorManager.unregisterListener(this);
    		audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC, nuLydStyrke, AudioManager.FLAG_SHOW_UI);
 //   		enLyd.release();
    	}

    // indre klasse
    //class Sensorlytter implements SensorEventListener {
    	public void onSensorChanged(SensorEvent event) {
    		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
    			float[] values = event.values;
    			// Movement
    			float x = values[0];
    			float y = values[1];
    			float z = values[2];

    			float accelationSquareRoot = (x * x + y * y + z * z)
    					/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
    			long actualTime = System.currentTimeMillis();
    			if (accelationSquareRoot >= 2) //
    			{
    				if (actualTime - lastUpdate < 400) {
    					return;
    				}
    				lastUpdate = actualTime;

    				Toast.makeText(this, "Ha' ska' ha' en OST", Toast.LENGTH_SHORT)
					.show();
    				
    				enLyd.start();
    			}

    		}
    		}

    	public void onAccuracyChanged(Sensor sensor, int præcision) {
    		// ignorér - men vi er nødt til at have metoden for at implementere interfacet
    	}
    	// }; // Sensorlytter slut
    }

